﻿using BlueDot.Mobile.Core;
using System;
using SqlMergeReplication.Controllers;


namespace SqlMergeReplication.Views
{
    public partial class SynchronizeView : ViewBase
    {
        private const string SynchronizeViewShown = WellKnownActions.ViewShown + "/SynchronizeView";

        #region Constructors

        public SynchronizeView()
        {
            InitializeComponent();
        }

        [DependencyConstructor]
        public SynchronizeView(IMobileApplication application)
            : base(application)
        {
            InitializeComponent();
        }

        #endregion

        [Receiver(SynchronizeViewShown)]
        public void SynchronizeViewShownAction(object sender, EventArgs e)
        {
            Shell shell = Application.Shell as Shell;
            if (shell != null)
            {
                shell.Menu = this.mainMenu1;
                shell.Text = this.Text;
            }
            this.statusTextBox.Text = string.Empty;
            this.StateBag[ApplicationStateBagItems.SYNC_STATUS].Value = string.Empty;
        }

        [DependencyProperty]
        public SyncController Controller { get; set; }

        private void runButton_Click(object sender, EventArgs e)
        {
            this.statusTextBox.Text = string.Empty;
            ToggleButtons();           
            this.Controller.Synchronize();

        }

        [Receiver(SyncController.SyncCompletedAction)]
        public void Controller_SynchronizationCompleted(object sender, EventArgs e)
        {            
            ToggleButtons();
        }

        private void ToggleButtons()
        {
            this.runButton.Enabled =
                this.synchronizeMenuItem.Enabled = !this.runButton.Enabled;
        }

        [StateBag(ApplicationStateBagItems.SYNC_STATUS, UpdateMode = UpdateMode.WhenActive)]
        public string Messages
        {
            set
            {
               this.statusTextBox.Text = value;
            }
        }
    }
}